#  Copyright (c) 2010
#  The Regents of the University of Michigan
#  All Rights Reserved

#  Permission is granted to use, copy, create derivative works, and
#  redistribute this software and such derivative works for any purpose,
#  so long as the name of the University of Michigan is not used in
#  any advertising or publicity pertaining to the use or distribution
#  of this software without specific, written prior authorization. If
#  the above copyright notice or any other identification of the
#  University of Michigan is included in any copy of any portion of
#  this software, then the disclaimer below must also be included.

#  This software is provided as is, without representation or warranty
#  of any kind either express or implied, including without limitation
#  the implied warranties of merchantability, fitness for a particular
#  purpose, or noninfringement.  The Regents of the University of
#  Michigan shall not be liable for any damages, including special,
#  indirect, incidental, or consequential damages, with respect to any
#  claim arising out of or in connection with the use of the software,
#  even if it has been or is hereafter advised of the possibility of
#  such damages.
import matplotlib
matplotlib.use('cairo.pdf')
BIG = False
if BIG:
  matplotlib.rcParams['font.size'] = 22
from matplotlib import pyplot

import numpy

import cPickle as pickle

plotdat = 'torrent_pop_plot.plotdat'

def plot_torrents_by_pop():
  data = [x[1] for x in pickle.load(open('content_to_ip.dat'))]
  data.reverse()
  ind = range(len(data))
  data = numpy.array(data[:1000] + data[1000:10000:10] + data[10000:100000:100] + data[100000::100], numpy.int32)
  ind = ind[:1000] + ind[1000:10000:10] + ind[10000:100000:100] + ind[100000::100]
    
  print len(data)
  pyplot.loglog(ind, data, linestyle='.', marker='.', color='k')
  pyplot.xlabel('Popularity rank')
  #pyplot.xlim(xmax=5000)
  pyplot.ylabel('# of IPs')
  pyplot.title('Torrent popularity distribution')

#  inset_ax = pyplot.axes([.25, .2, .6, .6])
#  pyplot.plot(data, linestyle='None', marker='.', color='k')
#  pyplot.xlim(xmax=500)

  if BIG:
    pyplot.savefig('torrent_pop_plot_loglog_big.pdf')
  else:
    pyplot.savefig('torrent_pop_plot_loglog.pdf')

  
if __name__ == '__main__':
  plot_torrents_by_pop()
